package com.link.base.tencent.corpwx.agent.util;

import com.alibaba.fastjson.JSONObject;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.HttpRequestUtil;
import com.link.core.util.StringUtils;

/**
 * 企业微信Token和ticket接口
 *
 * @author 欧阳振强
 * @date 2019/1/2
 */
public class AgentTokenUtil {
    /**获取accessToken接口*/
    private static final String GET_ACCESS_TOKEN_API = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET";
    /**获取企业js_api_ticket接口*/
    private static final String GET_CORP_JS_TICKET_API = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN";
    /**获取应用js_api_ticket接口*/
    private static final String GET_AGENT_JS_TICKET_API = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=ACCESS_TOKEN&type=agent_config";

    /**获取应用sessionKey接口-微信小程序用*/
    private static final String GET_MP_SESSION_KEY_API = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token=ACCESS_TOKEN&js_code=CODE&grant_type=authorization_code";

    /**
     * 获取access_token
     *
     * @author 欧阳振强
     * @date 2019/1/2
     * @param corpId 企业Id
     * @param secret 应用密钥
     * @return accessToken
     */
    public static synchronized String getAccessToken(String corpId, String secret) throws Exception {
        if (StringUtils.isBlank(corpId)) {
            throw new BasicServiceException("corpId不能为空！");
        }
        if (StringUtils.isBlank(secret)) {
            throw new BasicServiceException("secret不能为空！");
        }
        String url = GET_ACCESS_TOKEN_API.replace("ID", corpId).replace("SECRET", secret);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        String accessToken = resultObj.getString("access_token");
        return accessToken;
    }

    /**
     * 获取企业的js_api_ticket
     *
     * @author 欧阳振强
     * @date 2019/1/2
     * @param accessToken
     */
    public static synchronized String getJsTicket(String accessToken) {
        String url = GET_CORP_JS_TICKET_API.replace("ACCESS_TOKEN", accessToken);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        String jsTicket = resultObj.getString("ticket");
        return jsTicket;
    }

    /**
     * 获取应用的js_api_ticket
     *
     * @author 李宗庭
     * @date 2019/1/24
     * @param accessToken
     */
    public static synchronized String getAgentJsTicket(String accessToken) {
        String url = GET_AGENT_JS_TICKET_API.replace("ACCESS_TOKEN", accessToken);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        String agentJsTicket = resultObj.getString("ticket");
        return agentJsTicket;
    }

    public static synchronized JSONObject codeToSession(String accessToken, String code) {
        String url = GET_AGENT_JS_TICKET_API.replace("ACCESS_TOKEN", accessToken).replace("CODE", code);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        return resultObj;
    }

    public static synchronized JSONObject codeToSessionOfMp(String accessToken, String code) {
        String url = GET_MP_SESSION_KEY_API.replace("ACCESS_TOKEN", accessToken).replace("CODE", code);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        JSONObject resultObj = JSONObject.parseObject(resultStr);
        Integer errcode = resultObj.getInteger("errcode");
        if (errcode != 0) {
            throw new BasicServiceException(resultObj.getString("errmsg"));
        }
        return resultObj;
    }
}
